﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace DockSample.DAO
{
    class OriginalNFRChartTypeDAO : BaseDAO
    {
        NFRChartPointDAO pDAO = new NFRChartPointDAO();
        public void AddOriginalNFRChartType(OriginalNFRChartType nfrChart)
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "insert into OriginalNFRChartType( id ,x1,x2,x3,type)  "
                    + "values (  @id ,@x1,@x2,@x3,@type)";
                cmd.Parameters.Add(new SQLiteParameter("id", nfrChart.id));
                cmd.Parameters.Add(new SQLiteParameter("x1", nfrChart.X1));
                cmd.Parameters.Add(new SQLiteParameter("x2", nfrChart.X2));
                cmd.Parameters.Add(new SQLiteParameter("x3", nfrChart.X3));
                cmd.Parameters.Add(new SQLiteParameter("type", nfrChart.GetType().ToString()));
                cmd.ExecuteNonQuery();
                this.SaveRelations(nfrChart);

            }
            catch (System.Exception ex)
            {
                throw ex;

            }
            finally
            {
                this.CloseConnection();
            }

        }

        public OriginalNFRChartType GetOriginalNFRChartTypeById(string id)
        {
            OriginalNFRChartType chart = null;
            try
            {

                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "select * from OriginalNFRChartType where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", id));
                SQLiteDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string type = reader["type"].ToString();

                    OriginalNFRChartType nfrChart=null;
                    if ("DockSample.TypeOne".Equals(type))
                    {
                        nfrChart = new TypeOne();
                    }
                    else if ("DockSample.TypeTwo".Equals(type))
                    {
                        nfrChart = new TypeTwo();

                    }
                    else if ("DockSample.TypeThree".Equals(type))
                    {
                        nfrChart = new TypeThree();

                    }
                    else if ("DockSample.TypeFour".Equals(type))
                    {
                        nfrChart = new TypeFour();

                    }

                    else if ("DockSample.TypeFive".Equals(type))
                    {
                        nfrChart = new TypeFive();
                    }
                    else if ("DockSample.TypeSix".Equals(type))
                    {
                        nfrChart = new TypeSix();
                    }
                    else if ("DockSample.TypeSeven".Equals(type))
                    {
                        nfrChart = new TypeSeven();
                    }

                    nfrChart.id = reader["id"].ToString();
                    nfrChart.X1 = Double.Parse(reader["x1"].ToString());
                    nfrChart.X2 = Double.Parse(reader["x2"].ToString());
                    nfrChart.X3 = Double.Parse(reader["x3"].ToString());

                    nfrChart.pointList = pDAO.GetNFRChartPointByChartId(nfrChart.id);

                    chart = nfrChart;
                }
            }
            catch (System.Exception ex)
            {

                throw ex;
            }
            finally
            {
                this.CloseConnection();
            }
            return chart;
        }

        private void SaveRelations(OriginalNFRChartType nfrChart)
        {
            SavePoints(nfrChart);
        }

        private void SavePoints(OriginalNFRChartType nfrChart)
        {
            
            foreach (NFRChartPoint pt in nfrChart.pointList)
            {
                pDAO.AddNFRChartPoint(pt,nfrChart.id);
            }
        }


        
    }
}
